package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.RoutineStepRecordMapper;
import com.zhijian.medical.entity.po.RoutineStepRecord;
import com.zhijian.medical.entity.vo.request.RoutineStepRecordSearchVo;
import com.zhijian.medical.entity.vo.request.RoutineStepRecordSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.response.RoutineStepRecordVo;
import com.zhijian.medical.service.RoutineStepRecordService;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.AssertUtil;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 常规病理-流程状态历史业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-07-24 19:51:28
 */
@Slf4j
@Service
public class RoutineStepRecordServiceImpl implements RoutineStepRecordService {

	@Autowired
    private RoutineStepRecordMapper mapper;

    @Override
    public RoutineStepRecord selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public RoutineStepRecordVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(RoutineStepRecord routineStepRecord) {
        Date now = new Date();
        routineStepRecord.setCreateTime(now);
        routineStepRecord.setUpdateTime(now);
        return mapper.insert(routineStepRecord) > 0 ? routineStepRecord.getId() : 0L;
    }

    @Override
    public Long insertSelective(RoutineStepRecord routineStepRecord) {
        Date now = new Date();
        routineStepRecord.setCreateTime(now);
        routineStepRecord.setUpdateTime(now);
        return mapper.insertSelective(routineStepRecord) > 0 ? routineStepRecord.getId() : 0L;
    }

    @Override
    public boolean update(RoutineStepRecord routineStepRecord) {
        Date now = new Date();
        routineStepRecord.setUpdateTime(now);
        return mapper.updateByPrimaryKey(routineStepRecord) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(RoutineStepRecord record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(RoutineStepRecordSaveOrUpdateVo saveOrUpdateVo) {
        RoutineStepRecord routineStepRecord = new RoutineStepRecord();
        BeanUtils.copyProperties(saveOrUpdateVo, routineStepRecord);
        return this.insert(routineStepRecord) != 0;
    }

    @Override
    public boolean modify(RoutineStepRecordSaveOrUpdateVo saveOrUpdateVo) {
        RoutineStepRecord old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        RoutineStepRecord routineStepRecord = new RoutineStepRecord();
        BeanUtils.copyProperties(saveOrUpdateVo, routineStepRecord);
        return this.update(routineStepRecord);
    }

    @Override
    public RoutineStepRecordVo selectOne(RoutineStepRecordSearchVo routineStepRecordSearchVo) {
        return mapper.selectOne(routineStepRecordSearchVo);
     }

    @Override
    public Integer count(RoutineStepRecordSearchVo routineStepRecordSearchVo) {
        return mapper.count(routineStepRecordSearchVo);
    }

    @Override
    public List<RoutineStepRecordVo> selectList(RoutineStepRecordSearchVo routineStepRecordSearchVo) {
        return mapper.selectList(routineStepRecordSearchVo);
    }

    @Override
    public Page<RoutineStepRecordVo> selectPage(RoutineStepRecordSearchVo routineStepRecordSearchVo) {
        AssertUtil.check(Objects.isNull(routineStepRecordSearchVo.getPageNum()) || Objects.isNull(routineStepRecordSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(routineStepRecordSearchVo);
        //分页
        Page<RoutineStepRecordVo> page = new Page<>(true, count, routineStepRecordSearchVo.getPageNum(), routineStepRecordSearchVo.getPageSize());
        routineStepRecordSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<RoutineStepRecordVo> responseVos = this.selectList(routineStepRecordSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<RoutineStepRecord> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(RoutineStepRecordSearchVo routineStepRecordSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(routineStepRecordSearchVo);
    }
    
}